/*
 * @Author: panzhiwei 749458928@qq.com
 * @Date: 2024-01-29 14:53:20
 * @LastEditors: panzhiwei 749458928@qq.com
 * @LastEditTime: 2024-02-02 11:17:55
 * @FilePath: /2DDemo/assets/Script/weapon/RocketCrossbow.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 火箭弩类

// RocketCrossbow.ts
import { _decorator, Vec3, Collider2D, IPhysics2DContact, instantiate, Prefab, find } from 'cc';
import { Arrow } from './Arrow';
// import { SmallRocketSpawner } from '../SmallRocketSpawner';
import { Enemy } from '../Enemy/Enemy';
import { ArrowSpawner } from './ArrowSpawner'; // 确保路径正确
const { ccclass, property } = _decorator;

// 火箭弩-可穿透
@ccclass('RocketCrossbow')
export class RocketCrossbow extends Arrow {
    @property
    public explosionRadius: number = 100; // 火箭弩的爆炸半径

    // @property({ type: Prefab })
    // smallRocketPrefab: Prefab = null; // 小火箭弩的预制体

    // 弓箭生成器
    // @property({ type: ArrowSpawner })
    // public arrowSpawner: ArrowSpawner | null = null;

    // 可以添加火箭弩特有的属性和方法

    onLoad() {
        super.onLoad(); // 调用父类的 onLoad 方法
        // 初始化火箭弩特有的逻辑
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        // super.onBeginContact(selfCollider, otherCollider, contact); // 调用父类的 onBeginContact 方法

        // 检查是否与敌人碰撞
        if (!this.isDestroyed && otherCollider.node.name === 'Enemy') {

            setTimeout(() => {
                if (otherCollider && otherCollider.node) {
                    let enemyScript = otherCollider.getComponent(Enemy);
                    if (enemyScript) {
                        let isDestroyed = enemyScript.explosionTakeDamage(this.attackPower);
                        if (isDestroyed) {
                            // const spawnerNode = find('Canvas/SmallRocketSpawner'); // 替换为实际节点名
                            // const spawner = spawnerNode.getComponent(SmallRocketSpawner);
                            // 生成并发射三个小火箭弩

                            // if (this.arrowSpawner) {
                            //     let position = otherCollider.node.position;
                            //     this.arrowSpawner.smallRoketCrossbowShoot(position,3);
                            // }

                            const spawnerNode = find('Canvas/ArrowSpawner');
                            if (spawnerNode) {
                                const spawner = spawnerNode.getComponent(ArrowSpawner);
                                if (spawner) {
                                    let position = otherCollider.node.position;
                                    spawner.smallRoketCrossbowShoot(position,3);
                                }
                            }

                            if (selfCollider && selfCollider.node && selfCollider.node.isValid) {
                                // selfCollider.node.active = false;
                                this.isDestroyed = true;
                                // 销毁弓箭
                                selfCollider.node.destroy();
                            }
                        }
                    }
                }
            }, 0);
        }
    }

    // spawnAndShootSmallRocket(position: Vec3, index: number) {
    //     // 实例化小火箭弩
    //     // 在方法内部导入 SmallRocket 类
    //     const smallRocket = instantiate(this.smallRocketPrefab);
    //     smallRocket.setParent(this.node.parent); // 设置父节点
    //     smallRocket.setPosition(position); // 设置位置

    //     // 计算发射方向（示例：根据 index 设置不同的发射角度）
    //     let angle = index * 120; // 三个火箭弩分别相差120度
    //     let direction = new Vec3(Math.cos(angle), Math.sin(angle), 0);

    //     // 设置小火箭弩的属性和发射
    //     let smallRocketScript = smallRocket.getComponent(RocketCrossbow);
    //     if (smallRocketScript) {
    //         smallRocketScript.attackPower = this.attackPower / 2; // 设置攻击力
    //         smallRocketScript.shoot(direction); // 发射小火箭弩
    //     }
    // }

    // 可以添加实现火箭弩特殊效果的方法，例如爆炸效果
    createExplosion(position: Vec3, radius: number) {
        // 在此实现爆炸效果的逻辑
        // ...
    }
}

